package ar.com.laley.centro.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import ar.com.laley.centro.common.Constantes;
import ar.com.laley.centro.forms.DatosConsultaForm;
import ar.com.laley.centro.forms.ListaForm;
import ar.com.laley.centro.servicios.ServicioConsulta;
import ar.com.laley.centro.utils.AdministradorDeFechas;

public class ConsultaAction extends GenericAction {

    public ActionForward realizarConsulta(ActionMapping mapping,
	    ActionForm form, HttpServletRequest request,
	    HttpServletResponse response) throws Exception {

	ActionForward forward = null;
	DatosConsultaForm datosConsultaForm = (DatosConsultaForm) form;
	try {
	    if (ServicioConsulta.ingresarConsulta(datosConsultaForm)) {
		forward = mapping.findForward(Constantes.EXITO);
	    } else {
		throw new Exception();
	    }
	} catch (Exception e) {
	    e.printStackTrace();
	    forward = mapping.findForward(Constantes.ERROR);
	}
	return forward;
    }

    public ActionForward exportarConsulta(ActionMapping mapping,
	    ActionForm form, HttpServletRequest request,
	    HttpServletResponse response) throws Exception {

	ActionForward forward = null;
	ListaForm listaForm = (ListaForm) form;
	List lista = null;

	int desde = Integer.valueOf(listaForm.getListaFormDesde()).intValue();
	int hasta = Integer.valueOf(listaForm.getListaFormHasta()).intValue();

	try {
	    lista = ServicioConsulta.listarConsultas(desde, hasta);

	    if (lista.size() > 0) {

		MessageResources msg = getResources(request);

		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-disposition",
			"attachment;filename=" + msg.getMessage("file.name")
				+ "-" + AdministradorDeFechas.now() + ".xls");
		response.setHeader("Content-Encoding", "utf-8");
		response
			.setHeader("Expires: 0",
				"Cache-Control: must-revalidate, post-check=0, pre-check=0");

		String fDesde = AdministradorDeFechas.formatearFecha(desde);
		String fHasta = AdministradorDeFechas.formatearFecha(hasta);

		request.setAttribute("fechaDesde", fDesde);
		request.setAttribute("fechaHasta", fHasta);

		listaForm.setListaFormConsultas(lista);
		forward = mapping.findForward(Constantes.EXITO);
	    } else {
		forward = mapping.findForward(Constantes.ERROR);
	    }
	} catch (Exception e) {
	    forward = mapping.findForward(Constantes.ERROR);
	}
	return forward;
    }

    public ActionForward listarConsultasHechas(ActionMapping mapping,
	    ActionForm form, HttpServletRequest request,
	    HttpServletResponse response) throws Exception {

	ActionForward forward = null;
	ListaForm listaForm = (ListaForm) form;

	List lista = null;
	try {
	    int desde = Integer.valueOf(listaForm.getListaFormDesde())
		    .intValue();
	    int hasta = Integer.valueOf(listaForm.getListaFormHasta())
		    .intValue();
	    
	    lista = ServicioConsulta.listarConsultas(desde, hasta);
	    
	    if (lista.size() > 0){
	    	listaForm.setListaFormConsultas(lista);
	    }
	    
	    forward = mapping.findForward(Constantes.EXITO);
	} catch (Exception e) {
	    forward = mapping.findForward(Constantes.ERROR_MOSTRAR_CONSULTAS);
	}
	return forward;
    }

    public ActionForward listarMaterias(ActionMapping mapping, ActionForm form,
	    HttpServletRequest request, HttpServletResponse response)
	    throws Exception {

	ActionForward forward = null;
	DatosConsultaForm DatosConsultaForm = (DatosConsultaForm) form;
	List lista = null;
	try {
	    lista = ServicioConsulta.listarMaterias();
	    DatosConsultaForm.setFormSeleccionMateriaCPC(lista);

	    forward = mapping.findForward(Constantes.EXITO);
	} catch (Exception e) {
	    forward = mapping.findForward(Constantes.ERROR_MOSTRAR_CONSULTAS);
	}
	return forward;
    }
}
